<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Mod_option extends PNC_Model {

	public function get_position() {
		$query = $this->db
				->select('*')
				->from('pnc_position')
				->order_by('pos_name')
				->get();
		$position[0] = '-- Select --';
		foreach ($query->result_array() as $row) {
			$position[$row['pos_id']] = $row['pos_name'];
		}
		return $position;
	}

	public function get_role() {
		$query = $this->db
				->select('*')
				->from('pnc_roles')
				->order_by('role_name')
				->get();
		$role[0] = '-- Select --';
		foreach ($query->result_array() as $row) {
			$role[$row['role_id']] = $row['role_name']; //get id show name
		}
		return $role;
	}

	public function get_department() {
		$query = $this->db
				->select('*')
				->from('pnc_department')
				->where('sub_dept', 0)
				->order_by('dept_name')
				->get();
		$dept[0] = '-- Select --';
		foreach ($query->result_array() as $row) {
			$dept[$row['dept_id']] = $row['dept_name'];
		}
		return $dept;
	}

	public function get_manager() {
		$query = $this->db
				->select('*')
				->from('pnc_employee')
				->order_by('emp_lname')
				->where('role_id', 2)
				->or_where('role_id', 3)
				->or_where('role_id', 4)
				->get();
		$manager[0] = '-- Select --';
		foreach ($query->result_array() as $row) {
			$manager[$row['staff_id']] = ucfirst($row['emp_lname']) . ' ' . strtoupper($row['emp_fname']);
		}
		return $manager;
	}

	/* Get Staff id for add new record for instead */

	public function get_staff_name() {
		$query = $this->db
				->select('*')
				->from('pnc_employee')
				->join('pnc_roles', 'pnc_employee.role_id = pnc_roles.role_id')
				->order_by('emp_lname')
				->where('pnc_roles.role_alias', 'staff')
				->get();
		$staff[0] = '-- Select --';
		foreach ($query->result_array() as $row) {
			$staff[$row['staff_id']] = strtoupper($row['emp_lname']) . ' ' . ucfirst($row['emp_fname']);
		}
		return $staff;
	}

	public function get_group() {
		$query = $this->db->get('pnc_group');
		$group[0] = '-- Select --';
		foreach ($query->result_array() as $rows) {
			$group[$rows['grp_id']] = $rows['grp_name'];
		}
		return $group;
	}

	public function get_leaving_type() {
		$query = $this->db->get('pnc_leave_type');
//		$leaving[0] = '--- Select One ---';
		foreach ($query->result_array() as $row) {
			$leaving[$row['lea_id']] = $row['lea_name'];
		}
		return $leaving;
	}

	public function get_sub_leaving() {
		$query = $this->db->get('pnc_sub_leave');
		$sub[0] = '--- Select One ---';
		foreach ($query->result_array() as $row) {
			$sub[$row['stl_id']] = $row['stl_name'];
		}
		return $sub;
	}

	public function get_ssl_type() {
		$query = $this->db->get('pnc_ss_leave');
		$sstype[0] = '-- Select --';
		foreach ($query->result_array() as $row) {
			$sstype[$row['ssl_id']] = $row['ssl_name'];
		}
		return $sstype;
	}

	public function get_validator() {
		$query = $this->db
				->select('*')
				->from('pnc_employee')
				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
				->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
				->order_by('emp_fname')
				->where('pnc_roles.role_alias', 'gm')
				->or_where('pnc_roles.role_alias', 'manager')
				->or_where('pnc_roles.role_alias', 'submanager')
				->or_where('pnc_roles.role_alias', 'admin')
				->get();
		$val[0] = '-- select --';
		foreach ($query->result_array() as $row) {
			$val[$row['staff_id']] = ucfirst($row['emp_fname']) . ' ' . strtoupper($row['emp_lname']);
		}
		return $val;
	}

	/* Get email of validator */

	public function get_manage_mail($pos_name, $user_id) {
		$query = '';
		if ($pos_name === 'manager') {
			if ($this->session->userdata('staff')) {
				$query = $this->db
						->select('*')
						->from('pnc_employee')
						->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
						->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
						->order_by('emp_fname')
						->where('role_alias', $pos_name)
						->or_where('role_alias', 'submanager')
						->get();
			} else if ($this->session->userdata('submanager')) {
				$query = $this->db
						->select('*')
						->from('pnc_employee')
						->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
						->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
						->order_by('emp_fname')
						->where('role_alias', $pos_name)
						->or_where('role_alias', 'gm')
						->get();
			} else if ($this->session->userdata('admin')) {
				$query = $this->db
						->select('*')
						->from('pnc_employee')
						->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
						->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
						->order_by('emp_fname')
						->where('role_alias', 'admin')
						->where('staff_id !=', $user_id)
						->get();
			} else if ($this->session->userdata('gm')) {
				$query = $this->db
						->select('*')
						->from('pnc_employee')
						->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
						->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
						->order_by('emp_fname')
						->where('role_alias', 'admin')
						->get();
			} else if ($this->session->userdata('manager')) {
				$query = $this->db
						->select('*')
						->from('pnc_employee')
						->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
						->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
						->order_by('emp_fname')
						->where('role_alias', 'gm')
						->get();
			}
			$val[0] = '--- Select ---';
			foreach ($query->result_array() as $row) {
				$val[$row['emp_email']] = ucfirst($row['emp_fname']) . ' ' . strtoupper($row['emp_lname']);
			}
			return $val;
		} else if ($pos_name === 'gm') {
			if ($this->session->userdata('manager')) {
				$query = $this->db
						->select('*')
						->from('pnc_employee')
						->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
						->join('pnc_position', 'pnc_position.pos_id = pnc_employee.pos_id')
						->order_by('emp_fname')
						->where('role_alias', $pos_name)
						->get();
			}
			$val[0] = '--- Select ---';
			foreach ($query->result_array() as $row) {
				$val[$row['emp_email']] = ucfirst($row['emp_fname']) . ' ' . strtoupper($row['emp_lname']);
			}
			return $val;
		}
	}

	/* Get Sub Department */

	public function get_sub_department($get_department) {
		$query = $this->db
				->select('*')
				->from('pnc_department')
				->where('sub_dept', $get_department)
				->get();
		return $query;
	}

	public function get_subdepartment() {
		$query = $this->db
				->select('*')
				->from('pnc_department')
				->where('sub_dept !=', 0)
				->get();
		$val[0] = '--- Select ---';
		foreach ($query->result_array() as $row) {
			$val[$row['dept_id']] = ucfirst($row['dept_name']);
		}
		return $val;
	}

	/* Get sub department for editing */

	public function get_subdept($dept_id) {
		$query = $this->db
				->select('*')
				->from('pnc_department')
				->where('pnc_department.dept_id', $dept_id)
				->get();
		return $query;
	}

	/* Get only id of sub department for employee */

	public function get_subdept_id($id) {
		$query = $this->db
				->select('*')
				->from('pnc_employee')
				->where('pnc_employee.staff_id', $id)
				->get();
		return $query;
	}

	/* Check is admin */

//	public function is_admin($id) {
//		$query = $this->db
//				->select('*')
//				->from('pnc_employee')
//				->join('pnc_roles', 'pnc_roles.role_id = pnc_employee.role_id')
//				->where('role_alias', 'admin')
//				->where('staff_id', $id)
//				->get();
//		if ($query->num_rows() > 0) {
//			foreach ($query->result_array() as $rows) {
//				$is_admin[0] = $rows['role_alias'];
//			}
//			return $is_admin[0];
//		}
//		return FALSE;
//	}
}
